package redis

import (
	"context"
	"fmt"
	"time"
)

// 所有的group
const (
	DEVICE_UNBIND         = "device:unbind"
	DEVICE_BIND           = "device:bind"
	DEVICE_BIND_4G        = "device:bind:4G"
	DEVICE_BIND_BLUETOOTH = "device:bind:bluetooth"
	DEVICE_LOST           = "device:lost"
	DEVICE_FAULT          = "device:fault"
	RIDE_DISTANCE         = "device:ridedistance"
)

type ebikeUnbind struct {
}

type ebikeBind struct {
}

type ebikeBind4G struct {
}
type ebikeBindBluetooth struct {
}

type ebikeLost struct {
}

type ebikeFault struct {
}

type rideDistance struct {
}

var UnbindCache = &ebikeUnbind{}
var BindCache = &ebikeBind{}
var Bind4GCache = &ebikeBind4G{}
var BindBluetoothCache = &ebikeBindBluetooth{}
var LostCache = &ebikeLost{}
var FaultCache = &ebikeFault{}
var RideDistanceCache = &rideDistance{}

// 下个版本使用 没有绑定车辆
func (e *ebikeUnbind) GetTotal(ctx context.Context, key time.Time) (int64, error) {
	return DashboardCache.GetTotal(ctx, DEVICE_UNBIND, "total:"+key.Format("2006-01"))
}

func (e *ebikeUnbind) AddTotal(ctx context.Context, key time.Time, count int64) error {
	return DashboardCache.AddTotal(ctx, DEVICE_UNBIND, "total:"+key.Format("2006-01"), count)
}

func (e *ebikeUnbind) AddOneDay(ctx context.Context, key time.Time, count int64) (int64, error) {
	ketStr := key.Format("2006-01")
	return DashboardCache.AddOneDayCount(ctx, DEVICE_UNBIND, ketStr, count)

}
func (e *ebikeUnbind) GetOneDay(ctx context.Context, key time.Time) (int64, error) {
	keyStr := key.Format("2006-01")
	dayOfMonth := key.Day()
	// 月份天数都是从1开始的，list从0开始
	return DashboardCache.GetOneDayCount(ctx, DEVICE_UNBIND, keyStr, int64(dayOfMonth-1))
}

func (e *ebikeUnbind) AddThirtyDays(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.AddBatch(ctx, DEVICE_UNBIND, keyStr, counts...)
}

// GetThirtyDays 获取当前月份所有的天数数据
func (e *ebikeUnbind) GetThirtyDays(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.GetListItems(ctx, DEVICE_UNBIND, keyStr)
}

// AddTwelveMonths 获取当前年所有月份数据
func (e *ebikeUnbind) AddTwelveMonths(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.AddBatch(ctx, DEVICE_UNBIND, keyStr, counts...)
}

// GetTwelveMonths 获取当前年所有月份数据
func (e *ebikeUnbind) GetTwelveMonths(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.GetListItems(ctx, DEVICE_UNBIND, keyStr)
}

// AddFiveYears 获取当前年所有月份数据
func (e *ebikeUnbind) AddFiveYears(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	// 前面4年的数据是已经确定的，年度数据实时计算redis内部缓存数据
	// 往前推4年
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.AddBatch(ctx, DEVICE_UNBIND, keyStr, counts...)
}

func (e *ebikeUnbind) GetFiveYears(ctx context.Context, key time.Time) ([]int64, error) {
	// 按照从远到近依次返回数据，不包含今年的数据，今年数据实时计算
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.GetListItems(ctx, DEVICE_UNBIND, keyStr)
}

// 下个版本使用 没有绑定车辆
func (e *ebikeBind) GetTotal(ctx context.Context, key time.Time) (int64, error) {
	return DashboardCache.GetTotal(ctx, DEVICE_BIND, "total:"+key.Format("2006-01"))
}

func (e *ebikeBind) AddTotal(ctx context.Context, key time.Time, count int64) error {
	return DashboardCache.AddTotal(ctx, DEVICE_BIND, "total:"+key.Format("2006-01"), count)
}

// 下个版本使用 绑定车辆
func (e *ebikeBind) AddOneDay(ctx context.Context, key time.Time, count int64) (int64, error) {
	ketStr := key.Format("2006-01")
	return DashboardCache.AddOneDayCount(ctx, DEVICE_BIND, ketStr, count)

}
func (e *ebikeBind) GetOneDay(ctx context.Context, key time.Time) (int64, error) {
	keyStr := key.Format("2006-01")
	dayOfMonth := key.Day()
	// 月份天数都是从1开始的，list从0开始
	return DashboardCache.GetOneDayCount(ctx, DEVICE_BIND, keyStr, int64(dayOfMonth-1))
}

func (e *ebikeBind) AddThirtyDays(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.AddBatch(ctx, DEVICE_BIND, keyStr, counts...)
}

// GetThirtyDays 获取当前月份所有的天数数据
func (e *ebikeBind) GetThirtyDays(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.GetListItems(ctx, DEVICE_BIND, keyStr)
}

// AddTwelveMonths 获取当前年所有月份数据
func (e *ebikeBind) AddTwelveMonths(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.AddBatch(ctx, DEVICE_BIND, keyStr, counts...)
}

// GetTwelveMonths 获取当前年所有月份数据
func (e *ebikeBind) GetTwelveMonths(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.GetListItems(ctx, DEVICE_BIND, keyStr)
}

// AddFiveYears 获取当前年所有月份数据
func (e *ebikeBind) AddFiveYears(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	// 前面4年的数据是已经确定的，年度数据实时计算redis内部缓存数据
	// 往前推4年
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.AddBatch(ctx, DEVICE_BIND, keyStr, counts...)

}

func (e *ebikeBind) GetFiveYears(ctx context.Context, key time.Time) ([]int64, error) {
	// 按照从远到近依次返回数据，不包含今年的数据，今年数据实时计算
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.GetListItems(ctx, DEVICE_BIND, keyStr)
}

func (e *ebikeBind4G) GetTotal(ctx context.Context, key time.Time) (int64, error) {
	return DashboardCache.GetTotal(ctx, DEVICE_BIND_4G, "total:"+key.Format("2006-01"))
}

func (e *ebikeBind4G) AddTotal(ctx context.Context, key time.Time, count int64) error {
	return DashboardCache.AddTotal(ctx, DEVICE_BIND_4G, "total:"+key.Format("2006-01"), count)
}

// 下个版本使用
func (e *ebikeBind4G) AddOneDay(ctx context.Context, key time.Time, count int64) (int64, error) {
	ketStr := key.Format("2006-01")
	return DashboardCache.AddOneDayCount(ctx, DEVICE_BIND_4G, ketStr, count)

}
func (e *ebikeBind4G) GetOneDay(ctx context.Context, key time.Time) (int64, error) {
	keyStr := key.Format("2006-01")
	dayOfMonth := key.Day()
	// 月份天数都是从1开始的，list从0开始
	return DashboardCache.GetOneDayCount(ctx, DEVICE_BIND_4G, keyStr, int64(dayOfMonth-1))
}

func (e *ebikeBind4G) AddThirtyDays(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.AddBatch(ctx, DEVICE_BIND_4G, keyStr, counts...)
}

// GetThirtyDays 获取当前月份所有的天数数据
func (e *ebikeBind4G) GetThirtyDays(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.GetListItems(ctx, DEVICE_BIND_4G, keyStr)
}

// AddTwelveMonths 获取当前年所有月份数据
func (e *ebikeBind4G) AddTwelveMonths(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.AddBatch(ctx, DEVICE_BIND_4G, keyStr, counts...)
}

// GetTwelveMonths 获取当前年所有月份数据
func (e *ebikeBind4G) GetTwelveMonths(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.GetListItems(ctx, DEVICE_BIND_4G, keyStr)
}

// AddFiveYears 获取当前年所有月份数据
func (e *ebikeBind4G) AddFiveYears(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	// 前面4年的数据是已经确定的，年度数据实时计算redis内部缓存数据
	// 往前推4年
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.AddBatch(ctx, DEVICE_BIND_4G, keyStr, counts...)
}

func (e *ebikeBind4G) GetFiveYears(ctx context.Context, key time.Time) ([]int64, error) {
	// 按照从远到近依次返回数据，不包含今年的数据，今年数据实时计算
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.GetListItems(ctx, DEVICE_BIND_4G, keyStr)
}

func (e *ebikeBindBluetooth) GetTotal(ctx context.Context, key time.Time) (int64, error) {
	return DashboardCache.GetTotal(ctx, DEVICE_BIND_BLUETOOTH, "total:"+key.Format("2006-01"))
}

func (e *ebikeBindBluetooth) AddTotal(ctx context.Context, key time.Time, count int64) error {
	return DashboardCache.AddTotal(ctx, DEVICE_BIND_BLUETOOTH, "total:"+key.Format("2006-01"), count)
}

// 下个版本使用
func (e *ebikeBindBluetooth) AddOneDay(ctx context.Context, key time.Time, count int64) (int64, error) {
	ketStr := key.Format("2006-01")
	return DashboardCache.AddOneDayCount(ctx, DEVICE_BIND_BLUETOOTH, ketStr, count)

}
func (e *ebikeBindBluetooth) GetOneDay(ctx context.Context, key time.Time) (int64, error) {
	keyStr := key.Format("2006-01")
	dayOfMonth := key.Day()
	// 月份天数都是从1开始的，list从0开始
	return DashboardCache.GetOneDayCount(ctx, DEVICE_BIND_BLUETOOTH, keyStr, int64(dayOfMonth-1))
}

func (e *ebikeBindBluetooth) AddThirtyDays(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.AddBatch(ctx, DEVICE_BIND_BLUETOOTH, keyStr, counts...)
}

// GetThirtyDays 获取当前月份所有的天数数据
func (e *ebikeBindBluetooth) GetThirtyDays(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.GetListItems(ctx, DEVICE_BIND_BLUETOOTH, keyStr)
}

// AddTwelveMonths 获取当前年所有月份数据
func (e *ebikeBindBluetooth) AddTwelveMonths(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.AddBatch(ctx, DEVICE_BIND_BLUETOOTH, keyStr, counts...)
}

// GetTwelveMonths 获取当前年所有月份数据
func (e *ebikeBindBluetooth) GetTwelveMonths(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.GetListItems(ctx, DEVICE_BIND_BLUETOOTH, keyStr)
}

// AddFiveYears 获取当前年所有月份数据
func (e *ebikeBindBluetooth) AddFiveYears(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	// 前面4年的数据是已经确定的，年度数据实时计算redis内部缓存数据
	// 往前推4年
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.AddBatch(ctx, DEVICE_BIND_BLUETOOTH, keyStr, counts...)
}

func (e *ebikeBindBluetooth) GetFiveYears(ctx context.Context, key time.Time) ([]int64, error) {
	// 按照从远到近依次返回数据，不包含今年的数据，今年数据实时计算
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.GetListItems(ctx, DEVICE_BIND_BLUETOOTH, keyStr)
}

// 下个版本使用
func (e *ebikeLost) AddOneDay(ctx context.Context, key time.Time, count int64) (int64, error) {
	ketStr := key.Format("2006-01")
	return DashboardCache.AddOneDayCount(ctx, DEVICE_LOST, ketStr, count)

}
func (e *ebikeLost) GetOneDay(ctx context.Context, key time.Time) (int64, error) {
	keyStr := key.Format("2006-01")
	dayOfMonth := key.Day()
	// 月份天数都是从1开始的，list从0开始
	return DashboardCache.GetOneDayCount(ctx, DEVICE_LOST, keyStr, int64(dayOfMonth-1))
}

func (e *ebikeLost) AddThirtyDays(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.AddBatch(ctx, DEVICE_LOST, keyStr, counts...)
}

// GetThirtyDays 获取当前月份所有的天数数据
func (e *ebikeLost) GetThirtyDays(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.GetListItems(ctx, DEVICE_LOST, keyStr)
}

// AddTwelveMonths 获取当前年所有月份数据
func (e *ebikeLost) AddTwelveMonths(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.AddBatch(ctx, DEVICE_LOST, keyStr, counts...)
}

// GetTwelveMonths 获取当前年所有月份数据
func (e *ebikeLost) GetTwelveMonths(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.GetListItems(ctx, DEVICE_LOST, keyStr)
}

// AddFiveYears 获取当前年所有月份数据
func (e *ebikeLost) AddFiveYears(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	// 前面4年的数据是已经确定的，年度数据实时计算redis内部缓存数据
	// 往前推4年
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.AddBatch(ctx, DEVICE_LOST, keyStr, counts...)
}

func (e *ebikeLost) GetFiveYears(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.GetListItems(ctx, DEVICE_LOST, keyStr)
}

// 车辆丢失

// 下个版本使用
func (e *ebikeFault) AddOneDay(ctx context.Context, key time.Time, count int64) (int64, error) {
	ketStr := key.Format("2006-01")
	return DashboardCache.AddOneDayCount(ctx, DEVICE_FAULT, ketStr, count)

}
func (e *ebikeFault) GetOneDay(ctx context.Context, key time.Time) (int64, error) {
	keyStr := key.Format("2006-01")
	dayOfMonth := key.Day()
	// 月份天数都是从1开始的，list从0开始
	return DashboardCache.GetOneDayCount(ctx, DEVICE_FAULT, keyStr, int64(dayOfMonth-1))
}

func (e *ebikeFault) AddThirtyDays(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.AddBatch(ctx, DEVICE_FAULT, keyStr, counts...)
}

// GetThirtyDays 获取当前月份所有的天数数据
func (e *ebikeFault) GetThirtyDays(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.GetListItems(ctx, DEVICE_FAULT, keyStr)
}

// AddTwelveMonths 获取当前年所有月份数据
func (e *ebikeFault) AddTwelveMonths(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.AddBatch(ctx, DEVICE_FAULT, keyStr, counts...)
}

// GetTwelveMonths 获取当前年所有月份数据
func (e *ebikeFault) GetTwelveMonths(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.GetListItems(ctx, DEVICE_FAULT, keyStr)
}

// AddFiveYears 获取当前年所有月份数据
func (e *ebikeFault) AddFiveYears(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	// 前面4年的数据是已经确定的，年度数据实时计算redis内部缓存数据
	// 往前推4年
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.AddBatch(ctx, DEVICE_FAULT, keyStr, counts...)
}

func (e *ebikeFault) GetFiveYears(ctx context.Context, key time.Time) ([]int64, error) {
	// 按照从远到近依次返回数据，不包含今年的数据，今年数据实时计算
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.GetListItems(ctx, DEVICE_FAULT, keyStr)
}

// 骑行距离，只有总量的需求
func (e *rideDistance) AddOneDay(ctx context.Context, key time.Time, count int64) (int64, error) {
	ketStr := key.Format("2006-01")
	return DashboardCache.AddOneDayCount(ctx, RIDE_DISTANCE, ketStr, count)

}
func (e *rideDistance) GetOneDay(ctx context.Context, key time.Time) (int64, error) {
	keyStr := key.Format("2006-01")
	dayOfMonth := key.Day()
	// 月份天数都是从1开始的，list从0开始
	return DashboardCache.GetOneDayCount(ctx, RIDE_DISTANCE, keyStr, int64(dayOfMonth-1))
}

func (e *rideDistance) AddThirtyDays(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.AddBatch(ctx, RIDE_DISTANCE, keyStr, counts...)
}

// GetThirtyDays 获取当前月份所有的天数数据
func (e *rideDistance) GetThirtyDays(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006-01")
	return DashboardCache.GetListItems(ctx, RIDE_DISTANCE, keyStr)
}

// AddTwelveMonths 获取当前年所有月份数据
func (e *rideDistance) AddTwelveMonths(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.AddBatch(ctx, RIDE_DISTANCE, keyStr, counts...)
}

// GetTwelveMonths 获取当前年所有月份数据
func (e *rideDistance) GetTwelveMonths(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := key.Format("2006")
	return DashboardCache.GetListItems(ctx, RIDE_DISTANCE, keyStr)
}

// AddFiveYears 获取当前年所有月份数据
func (e *rideDistance) AddFiveYears(ctx context.Context, key time.Time, counts []int64) (int64, error) {
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.AddBatch(ctx, RIDE_DISTANCE, keyStr, counts...)
}

func (e *rideDistance) GetFiveYears(ctx context.Context, key time.Time) ([]int64, error) {
	keyStr := fmt.Sprintf("%s:%s", "years", key.Format("2006"))
	return DashboardCache.GetListItems(ctx, RIDE_DISTANCE, keyStr)
}
